//
//  AssetViewModel.m
//  AppleMarket
//
//  Created by yangy on 30/10/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "AssetViewModel.h"
// model
#import "CoinAssetModel.h"

@implementation AssetViewModel

- (instancetype)init
{
    self = [super init];
    if(self){
        [self.refershAssetCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
            if([[x objectForKey:@"Status"] boolValue]){
                NSArray *datas = [x objectForKey:@"Data"];
                self.bbConinModels = [[datas.rac_sequence map:^id _Nullable(id  _Nullable value) {
                    return [CoinAssetModel yy_modelWithDictionary:value];
                }] array];
                [self.refershEndSubject sendNext:nil];
            }
        }];
    }
    return self;
}

- (RACCommand *)refershAssetCommand
{
    if(!_refershAssetCommand){
        _refershAssetCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            // 币币账户用户资产
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                [NetRequest request_UserAssetListWithCompleteHandler:^(id  _Nonnull responseObj) {
                    [subscriber sendNext:responseObj];
                    [subscriber sendCompleted];
                } fail:^(NSError * _Nonnull error) {
                    [subscriber sendError:error];
                }];
                return nil;
            }];
        }];
    }
    return _refershAssetCommand;
}

- (RACSubject *)refershEndSubject
{
    if(!_refershEndSubject){
        _refershEndSubject = [RACSubject subject];
    }
    return _refershEndSubject;
}

- (RACSubject *)assetClickSubject
{
    if(!_assetClickSubject){
        _assetClickSubject = [RACSubject subject];
    }
    return _assetClickSubject;
}

@end
